import { getStoresByUserId, getHomePage } from '../../../services/api';

export default {
  namespace: 'merchant',

  state: {
    data: [],
  },

  effects: {
    *fetch(payload, { call, put }) {
      const userId = localStorage.getItem('userId');
      const response = yield call(getStoresByUserId, userId);
      localStorage.setItem('storeId', response.list[0].id);
      const homeInfo = yield call(getHomePage);
      yield put({
        type: 'save',
        payload: { response, homeInfo },
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        data: payload.response.list,
        homeInfo: payload.homeInfo,
      };
    },
  },
};
